package com.star.carservice.client.utils;

import com.star.carservice.client.AppController;

import android.content.Context;
import android.content.SharedPreferences;

public class PreferencesUtils {

	public static String PREFERENCE_NAME = "carservice";

	private static Context getApplicationContext() {
		return AppController.getInstance();
	}

	private static final String Key_UserId = "userId";
	private static final String Key_Mobile = "mobile";
	private static final String Key_AccessToken = "accesstoken";

	/**************************** start Preferences *************************************/

	public static String getUserId() {
		return getString(Key_UserId, "");
	}

	public static void setUserId(String userId) {
		putString(Key_UserId, userId.trim());
	}
	
	public static String getMobile() {
		return getString(Key_Mobile, "");
	}

	public static void setMobile(String mobile) {
		putString(Key_Mobile, mobile.trim());
	}

	
	public static String getAccessToken() {
		return getString(Key_AccessToken, "");
	}

	public static void setAccessToken(String accessToken) {
		putString(Key_AccessToken, accessToken.trim());
	}


	/**************************** end Preferences *************************************/

	public static boolean putString(String key, String value) {
		SharedPreferences settings = getApplicationContext()
				.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(key, value);
		return editor.commit();
	}

	public static String getString(String key) {
		return getString(key, null);
	}

	public static String getString(String key, String defaultValue) {
		SharedPreferences settings = getApplicationContext()
				.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
		return settings.getString(key, defaultValue);
	}

	public static boolean putInt(String key, int value) {
		SharedPreferences settings = getApplicationContext()
				.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt(key, value);
		return editor.commit();
	}

	public static int getInt(String key) {
		return getInt(key, -1);
	}

	public static int getInt(String key, int defaultValue) {
		SharedPreferences settings = getApplicationContext()
				.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
		return settings.getInt(key, defaultValue);
	}

	public static boolean putLong(String key, long value) {
		SharedPreferences settings = getApplicationContext()
				.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putLong(key, value);
		return editor.commit();
	}

	public static long getLong(String key) {
		return getLong(key, -1);
	}

	public static long getLong(String key, long defaultValue) {
		SharedPreferences settings = getApplicationContext()
				.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
		return settings.getLong(key, defaultValue);
	}

	public static boolean putFloat(String key, float value) {
		SharedPreferences settings = getApplicationContext()
				.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putFloat(key, value);
		return editor.commit();
	}

	public static float getFloat(String key) {
		return getFloat(key, -1);
	}

	public static float getFloat(String key, float defaultValue) {
		SharedPreferences settings = getApplicationContext()
				.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
		return settings.getFloat(key, defaultValue);
	}

	public static boolean putBoolean(String key, boolean value) {
		SharedPreferences settings = getApplicationContext()
				.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(key, value);
		return editor.commit();
	}

	public static boolean getBoolean(String key) {
		return getBoolean(key, false);
	}

	public static boolean getBoolean(String key, boolean defaultValue) {
		SharedPreferences settings = getApplicationContext()
				.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
		return settings.getBoolean(key, defaultValue);
	}
}
